package com.zl.controller;

import com.zl.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZLL
 * @date create in 2018/04/27
 */
@RestController
public class EnvController {

    @Autowired
    private Environment environment;

    @Autowired
    private ApplicationContext applicationContext;

    @RequestMapping("/get1")
    public void getEnv(){
        String val = environment.getProperty("contextConfigLocation");
        System.out.println(val);

        String v2 = environment.getProperty("spring.profiles.active");
        System.out.println(v2);

        //修改env
        boolean b = environment.acceptsProfiles("dev");
        if(b){
            System.out.println("ok");
        }

        boolean b2 = environment.acceptsProfiles("test");
        if(b2){
            System.out.println("ok");
        }

        ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;

        configurableEnvironment.setActiveProfiles("test");

        User bean = applicationContext.getBean("user22",User.class);
        System.out.println(bean);
    }
}
